package com.atguigu.guli.service.statistics.service.impl;

import com.atguigu.guli.service.base.feign.UcenterFeignClient;
import com.atguigu.guli.service.base.handler.GuliException;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.base.result.ResultCodeEnum;
import com.atguigu.guli.service.statistics.entity.Daily;
import com.atguigu.guli.service.statistics.mapper.DailyMapper;
import com.atguigu.guli.service.statistics.service.DailyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-12-15
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {
    @Autowired
    UcenterFeignClient ucenterFeignClient ;
    //保存统计数据到数据库
    @Override
    public void genStatistics(String day) {
        Daily daily = new Daily() ;
        daily.setDateCalculated(day) ;
        //远程访问ucenter服务
        R r = ucenterFeignClient.getRegisterNum(day);
        if (!r.getSuccess()) {
            throw new GuliException(ResultCodeEnum.UNKNOWN_REASON) ;
        }
        Integer num = Integer.parseInt(r.getData().get("num").toString()) ;
        daily.setRegisterNum(num) ;
        //生成模拟数据
        daily.setCourseNum(RandomUtils.nextInt(10,30)) ;
        daily.setLoginNum(RandomUtils.nextInt(1000,3000)) ;
        daily.setVideoViewNum(RandomUtils.nextInt(200,1000)) ;
        baseMapper.insert(daily) ;
    }

    @Override
    public Map getStatistics(String begin, String end) {
        LambdaQueryWrapper<Daily> queryWrapper = new LambdaQueryWrapper<>() ;
        queryWrapper.ge(Daily::getDateCalculated,begin) ;
        queryWrapper.le(Daily::getDateCalculated,end) ;
        queryWrapper.orderByAsc(Daily::getDateCalculated) ;
        List<Daily> dailies = baseMapper.selectList(queryWrapper);
        List registNums = new ArrayList() ;
        List loginNums = new ArrayList() ;
        List videoViewNums = new ArrayList() ;
        List courseNums = new ArrayList() ;
        List dates = new ArrayList<>();
        for (Daily daily : dailies) {
            registNums.add(daily.getRegisterNum()) ;
            loginNums.add(daily.getLoginNum()) ;
            videoViewNums.add(daily.getVideoViewNum()) ;
            courseNums.add(daily.getCourseNum()) ;
            dates.add(daily.getDateCalculated()) ;
        }

        Map<String, List<Integer>> map = new HashMap<>() ;
        map.put("registNums",registNums) ;
        map.put("loginNums",loginNums) ;
        map.put("videoViewNums",videoViewNums) ;
        map.put("courseNums",courseNums) ;
        map.put("dates" ,dates );
        return map;
    }
}
